/*--------------------------------------------------------------------------
 *  Copyright 2011 Taro L. Saito
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *--------------------------------------------------------------------------*/
//--------------------------------------
// XerialJ
//
// TextSplitLens.java
// Since: 2011/05/26 12:41:20
//
// $URL$
// $Author$
//--------------------------------------
package org.xerial.silk.weaver.lens;

import java.io.File;

import org.xerial.silk.weaver.cloud.Function;
import org.xerial.silk.weaver.cloud.Reporter;

/**
 * Lens for splitting large text into several chunks
 * 
 * @author leo
 * 
 */
public class TextSplitLens implements Function
{
    public File file;
    public int  blockSize;

    @Override
    public void execute(Reporter reporter) {

        long fileSize = file.getTotalSpace();
        long numSplits = (fileSize + blockSize - 1) / blockSize;

        for (long i = 0; i < numSplits; ++i) {
            long start = blockSize * i;
            long end = Math.min(blockSize * (i + 1), fileSize);
            reporter.emit(new TextSplit(file, start, (int) (end - start)));
        }
    }

    @Override
    public Class< ? > getResultType() {
        return TextSplit.class;
    }

}
